﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;
using Moon.DBUtility;

namespace GPI.Action
{
    public class Find
    {
        public static Regex test = new Regex("^[{][\\w:,]+[$}]");
        /// <summary>
        /// 根据条件返回相关列表
        /// </summary>
        /// <param name="table"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable GetTableByAnd(string table, string json)
        {
            GPI.Server.ActionServer service = new GPI.Server.ActionServer();
            if (test.IsMatch(json))
            {
                json=json.Remove(0, 1);
                json = json.Remove(json.Length - 1, 1);
                StringBuilder strWhere=new StringBuilder();
                strWhere.Append(" 1=1 ");
                string[] tmp = json.Split(',');
                foreach (string temp in tmp)
                {
                     strWhere.Append(" and "+temp.Split(':')[0]+"='"+temp.Split(':')[1]+"'");
                }
                return service.FindByStr(table, null, strWhere.ToString()+" order by id asc");
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 根据条件返回相关列表
        /// </summary>
        /// <param name="table"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable GetTableByOr(string table, string json)
        {
            GPI.Server.ActionServer service = new GPI.Server.ActionServer();
            if (test.IsMatch(json))
            {
                json = json.Remove(0, 1);
                json = json.Remove(json.Length - 1, 1);
                StringBuilder strWhere = new StringBuilder();
                string[] tmp = json.Split(',');
                foreach (string temp in tmp)
                {
                    strWhere.Append(temp.Split(':')[0] + "='" + temp.Split(':')[1] + "' or ");
                }
                strWhere.Append("1<>1");
                return service.FindByStr(table, null, strWhere.ToString());
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 根据列名返回列表
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="column">列名</param>
        /// <param name="value">值</param>
        /// <param name="isInt">是否是数字</param>
        /// <returns></returns>
        public static DataTable GetTableByColumn(string table, string column, string value,string isInt)
        {
            GPI.Server.ActionServer service = new GPI.Server.ActionServer();

                StringBuilder strWhere = new StringBuilder();
                if ("NO".Equals(isInt))
                {
                    strWhere.Append(" " + column + "='" + value + "'");
                }
                else {
                    strWhere.Append(" " + column + "=" + value);
                }
                return service.FindByStr(table, null, strWhere.ToString());
        }

        public static object GetModelByCoumn(string table, string column, string value, string isInt)
        {
            GPI.Server.ActionServer service = new GPI.Server.ActionServer();

            StringBuilder strWhere = new StringBuilder();
            if ("NO".Equals(isInt))
            {
                strWhere.Append(" " + column + "='" + value + "'");
            }
            else
            {
                strWhere.Append(" " + column + "=" + value);
            }
            DataTable ds=service.FindByStr(table, null, strWhere.ToString());
            return ds.Rows.Count > 0 ? ds.Rows[0] : null;
        }

        public static object GetModelByCoumn(string table, string column, string value, string isInt,string top)
        {
            GPI.Server.ActionServer service = new GPI.Server.ActionServer();

            StringBuilder strWhere = new StringBuilder();
            if ("NO".Equals(isInt))
            {
                strWhere.Append(" " + column + "='" + value + "'");
            }
            else
            {
                strWhere.Append(" " + column + "=" + value);
            }
            DataTable ds = service.FindByStr(table, null, strWhere.ToString());
            if(ds.Rows.Count<=top.ToInt(0))
            return ds.Rows.Count > 0 ? ds.Rows[0] : null;
            else
            return ds.Rows.Count > 0 ? ds.Rows[top.ToInt(0)] : null;
        }
    }
}
